package com.fick.tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;
import com.fick.util.General;
/**
 * 显示指定信息
 * @author 王浩强
 * 2009-08-18
 * 
 * 
 * **/
public class NewsShowTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsShowTag.class);
	
	//内容表格table属性
	private String tableAttr="";
	//标题td属性
	private String titleTdAttr="";
	//描述td属性
	private String aboutTdAttr="";
	//内容td属性
	private String contentTdAttr="";

	/**
	 * 显示收藏此页
	 */
	private String isSCPage = "true";

	/**
	 * 显示打印此页
	 */
	private String isPrintPage = "true";

	/**
	 * 显示关闭此页
	 */
	private String isClosePage = "true";
	//日期格式
	private String dateFormat="yyyy-MM-dd";
	public int doStartTag() throws JspException {
		
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String newsid=request.getParameter("newsid");
			StringBuffer sb = new StringBuffer();
			if (newsid!=null && newsid.trim().length()>0) {
				NewsDAO dao=new NewsDAO();
				int id=0;
				id=Integer.parseInt(newsid);
				News news=dao.findById(id);
				if (news!=null) {
					//增加点击量
					news.setClickNum(news.getClickNum()+1);
					dao.update(news);
					sb.append(" <OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2   height=0   width=0 VIEWASTEXT></OBJECT>");
					sb.append("<table ");
					sb.append(tableAttr);
					sb.append(" >");
					sb.append("<tr>");
					sb.append("<td ");
					sb.append(titleTdAttr);
					sb.append(" ><font face='宋体' size='5' color=red><br>");
					sb.append(news.getTitle());
					sb.append("</font></td></tr><tr>");
					sb.append("<td ");
					sb.append(aboutTdAttr);
					sb.append(" >");
					if ("true".equals(isSCPage)) {
						sb.append("【");
						sb.append("<a href='#' onclick=\"window.external.AddFavorite(location.href,document.title);\">");
						sb.append("收藏此页");
						sb.append("</a>");
						sb.append("】");
						sb.append("&nbsp;");
					}
					if ("true".equals(isPrintPage)) {
						sb.append("【");
						sb.append("<a href='#' onclick=\"document.all.WebBrowser.ExecWB(6,1)\">");
						sb.append("打印");
						sb.append("</a>");
						sb.append("】");
						sb.append("【");
						sb.append("<a href='#' onclick=\"document.all.WebBrowser.ExecWB(7,1)\">");
						sb.append("打印预览");
						sb.append("</a>");
						sb.append("】");
						sb.append("&nbsp;");
					}
					if ("true".equals(isClosePage)) {
						sb.append("【");
						sb.append("<a href='#' onclick=\"window.close();\">");
						sb.append("关闭");
						sb.append("</a>");
						sb.append("】");
						sb.append("&nbsp;"); 
					}
					sb.append("&nbsp;来源：");
					sb.append(news.getAdmin().getUnit());
					sb.append("&nbsp;发布人：");
					sb.append(news.getPublisher());
					sb.append("&nbsp;签发人：");
					sb.append(news.getReviewed());
									
					
					sb.append("&nbsp;日期:");
					SimpleDateFormat df = new SimpleDateFormat(dateFormat);
					sb.append(df.format(news.getAddDate()));
					sb.append("&nbsp;点击量:");
					sb.append(news.getClickNum());
					sb.append("</td>");
					sb.append("</tr><tr><td class=\"content_btx2\"></td></tr><tr>");
					sb.append("<td ");
					sb.append(contentTdAttr);
					sb.append(" ><br>");
					sb.append(news.getContent());
					//附件
					if (news.getAttachment()!=null && news.getAttachment().trim().length()>0) {
						sb.append("附件如下：<br>");
						String attach[]=news.getAttachment().split(";");
						for(int i=0;i<attach.length;i++)
						{
						sb.append("<a href='/downloadFile.jsp?filename=");
						sb.append(java.net.URLEncoder.encode(attach[i],"UTF-8"));
						sb.append("' >");
						sb.append(attach[i].substring(attach[i].lastIndexOf("/")+1,attach[i].length()));
						sb.append("</a><br>");
						}
					}
					sb.append("</td></tr>");
					sb.append("</table>");
				}
			}else {
				sb.append("没有获取参数!");
			}
			
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		}catch(IOException e)
		{
			log.error("标签显示错误"+e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	public String getTableAttr() {
		return tableAttr;
	}
	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}
	public String getTitleTdAttr() {
		return titleTdAttr;
	}
	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}
	public String getAboutTdAttr() {
		return aboutTdAttr;
	}
	public void setAboutTdAttr(String aboutTdAttr) {
		this.aboutTdAttr = aboutTdAttr;
	}
	public String getContentTdAttr() {
		return contentTdAttr;
	}
	public void setContentTdAttr(String contentTdAttr) {
		this.contentTdAttr = contentTdAttr;
	}
	public String getIsSCPage() {
		return isSCPage;
	}
	public void setIsSCPage(String isSCPage) {
		this.isSCPage = isSCPage;
	}
	public String getIsPrintPage() {
		return isPrintPage;
	}
	public void setIsPrintPage(String isPrintPage) {
		this.isPrintPage = isPrintPage;
	}
	public String getIsClosePage() {
		return isClosePage;
	}
	public void setIsClosePage(String isClosePage) {
		this.isClosePage = isClosePage;
	}
	public String getDateFormat() {
		return dateFormat;
	}
	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
}
